package com.android.winetopin;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import com.android.winetopin.cache.DiskLruCacheHelper;
import com.android.winetopin.model.group.MainSearchSortData;
import com.android.winetopin.utils.ConstantsUrl;
import com.android.winetopin.utils.MyLog;
import com.android.winetopin.model.group.MainSearchSortDataCallback;
import com.android.winetopin.utils.HttpUtils;
import com.android.winetopin.utils.ObjectUtils;
import com.android.winetopin.view.group.ViewPagerIndicator;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

public class WelcomeActivity  extends AppCompatActivity {
    public static WelcomeActivity intance = null;
    private WelcomeActivity mcontext;
    private DiskLruCacheHelper helper;
    private int[] Bannerlist = new int[5];
    private ViewPager bannerpager;
    private ViewPagerIndicator viewPagerIndicator1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        intance = this;
        mcontext = WelcomeActivity.this;
        ObjectUtils.StatusBarWhiteBg(mcontext, false);
        try {
            helper = new DiskLruCacheHelper(mcontext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        bannerpager = (ViewPager) findViewById(R.id.bannerpager_fragment);
        viewPagerIndicator1 = (ViewPagerIndicator) findViewById(R.id.viewPagerIndicator1);
        viewPagerIndicator1.autoScroll(getSupportFragmentManager(), bannerpager, 0);
        GetSortData();
        initData();
    }
    /**
     * 轮播图销毁开启
     */

    @Override
    public void onStart() {
        super.onStart();
        viewPagerIndicator1.setVisible(true);
    }


    @Override
    public void onPause() {
        super.onPause();
        viewPagerIndicator1.setVisible(false);

    }
    private void initData() {

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(mcontext, MainActivity.class));
                finish();

            }
        }, 3000);
    }

    private void GetSortData() {
        Bannerlist[0]=R.mipmap.welcomeone;
        Bannerlist[1]=R.mipmap.welcometwo;
        Bannerlist[2]=R.mipmap.welcomethree;
        Bannerlist[3]=R.mipmap.welcomefour;
        Bannerlist[4]=R.mipmap.welcomefive;
        viewPagerIndicator1.setViewPager3(Bannerlist);
        viewPagerIndicator1.setNotifyDataSetChanged();
        boolean networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.SORTLISTDATA;
        MyLog.e("地址", address);
        if (networkConnected) {
            HttpUtils.get(address, null, new MainSearchSortDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(MainSearchSortData response, int id) {
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH, response.toString());
                    }
                }
            });
        } else {

        }
    }
}
